﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.model;
using NTTDATA.BEENChina.ResultCertification.Checker;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.Main.ServiceResultCertification;

namespace NTTDATA.BEENChina.ResultCertification.UI
{
    public partial class FormCategoryModify : Form
    {       
        private List<CategoryConfig> editList = new List<CategoryConfig>();
        private bool changed = false;
            
        public FormCategoryModify()
        {
            InitializeComponent();
        }
            
        private void FormCategoryModify_Load(object sender, EventArgs e)
        {   
            this.dataGridCategory.Columns.Clear();
            fillCategoryGroup();            
        }   
            
        private void fillCategoryGroup()
        {            
            TreeNode parentNode = treeGroup.Nodes.Add("Cagegory Group");

            string[] fieldinfo = typeof(EnumCategoryType).GetEnumNames();
            foreach (String item in fieldinfo)
            {
                TreeNode node = parentNode.Nodes.Add(item);
                int enumValue = (int)Enum.Parse(typeof(EnumCategoryType), item);                
            }
            
            parentNode.Expand();
        }   

        private void treeGroup_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.dataGridCategory.CellValueChanged -= dataGridCategory_CellValueChanged;
            if (e.Node != null && e.Node.Parent != null)
            {
                EnumCategoryType categoryType = (EnumCategoryType)Enum.Parse(typeof(EnumCategoryType), e.Node.Text);

                IEnumerable<CategoryConfig> categoryList = null;
                if (e.Node.Tag == null)
                {
                    using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                    {
                        categoryList = client.GetCagetoryConfigs(categoryType);
                    }
                }
                else
                {
                    categoryList = (IEnumerable<CategoryConfig>)e.Node.Tag;
                }
                //e.Node.Tag != null ? (IEnumerable<CategoryConfig>)e.Node.Tag : (new CategoryConfigAccess()).GetCagetoryConfigs(categoryType);
                //IEnumerable<VerfityItem> verfityList = (new VerfityAccess()).GetVerfityItems(categoryType);
                IEnumerable<VerfityItem> verfityList = null;
                using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                {
                    verfityList = client.GetVerfityItems(categoryType);
                }
                e.Node.Tag = categoryList;

                iniGridColumn(verfityList);
                fillGrid(verfityList, categoryList);
            }
            this.dataGridCategory.CellValueChanged += dataGridCategory_CellValueChanged;
        }

        private void iniGridColumn(IEnumerable<VerfityItem> verfityList)
        {
            dataGridCategory.Columns.Clear();
            
            if (verfityList == null)
            {
                return;
            }
            
            DataGridViewTextBoxColumn categoryIdCol=new DataGridViewTextBoxColumn();
            categoryIdCol.HeaderText = "CategoryId";
            categoryIdCol.Width = 80;
            categoryIdCol.ReadOnly = true;
            dataGridCategory.Columns.Add(categoryIdCol);

            DataGridViewTextBoxColumn categoryName = new DataGridViewTextBoxColumn();
            categoryName.HeaderText = "CategoryName";
            categoryName.Width = 300;
            categoryName.ReadOnly = true;
            dataGridCategory.Columns.Add(categoryName);

            foreach (VerfityItem v in verfityList)
            {
                DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
                col.HeaderText = v.VerfityItemName;
                col.Tag = v;
                dataGridCategory.Columns.Add(col);
            }
        }

        private void fillGrid(IEnumerable<VerfityItem> verfityList, IEnumerable<CategoryConfig> categoryList)
        {
            if (verfityList == null || categoryList == null)
            {
                return;
            }
           
            dataGridCategory.RowCount = categoryList.Count();

            int i = -1;
            foreach (CategoryConfig category in categoryList)
            {
                i++;
                dataGridCategory.Rows[i].Cells[0].Value = category.CategoryId;
                dataGridCategory.Rows[i].Cells[1].Value = category.CategoryName;
                int j = 2;
                foreach (VerfityItem v in verfityList)
                {
                    bool checkVlaule = category.VerfityItems != null && category.VerfityItems.Any(o => o.VerfityItemId == v.VerfityItemId); ;
                    dataGridCategory.Rows[i].Cells[j++].Value = checkVlaule;                
                }
                dataGridCategory.Rows[i].Tag = category;
            }            
        }

        private void dataGridCategory_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            CategoryConfig category = (CategoryConfig)dataGridCategory.Rows[e.RowIndex].Tag;
            VerfityItem verfity = (VerfityItem)dataGridCategory.Columns[e.ColumnIndex].Tag;
            if (category.VerfityItems == null)
            {
                category.VerfityItems = new List<VerfityItem>();
            }

            bool value = (bool)dataGridCategory.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            (category.VerfityItems).RemoveAll(o => o.VerfityItemId == verfity.VerfityItemId);
            if (value == true)
            {
                ((List<VerfityItem>)category.VerfityItems).Add(verfity);
            }
            changed = true;
            editList.RemoveAll(o => o.CategoryId == category.CategoryId);
            editList.Add(category);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //CategoryConfigAccess access = new CategoryConfigAccess ();
            using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
            {
                foreach (CategoryConfig category in editList)
                {
                    //access.RemoveCategoryVerfities(category.CategoryId);
                    //access.AddCategoryVerfities(category.CategoryId, category.VerfityItems);
                    client.RemoveCategoryVerfities(category.CategoryId);
                    client.AddCategoryVerfities(category.CategoryId, category.VerfityItems);
                }
            }
            changed = false;
            MessageBox.Show("保存成功！","提示");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (changed == true)
            {
                if (MessageBox.Show("是否放弃更改？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    this.Close();
                }
                else
                {
                    return;
                }
            }
            this.Close();
        }
    }
}